define(function (require) {
    var Vue = require('vue'),
        $api = require('config'),
        ajax = require('ajax'),
        utilities = require('utilities'),
        spellSort = function (data) {
            var spell = {},
                tmp = [],
                exists = {};

            //按照spell第一位字母push arr
            data.forEach(function (item, key) {
                if (!exists[item.id] && item.uid != IGrow.user.uid) {
                    //是否是字母 ?
                    key = /^[a-z]/i.test(item.spell || '') ? item.spell.charAt(0).toUpperCase() : 0;
                    spell[key] = spell[key] || [];
                    spell[key].push(item);
                    exists[item.id] = true;
                }
            });

            //A-Z push->[] 未绑定在后
            for(key in spell){
                tmp.push({name:key, data:spell[key]});
            }

            //A-Z 排序
            tmp.sort(function (a, b) {
                return a.name < b.name ? -1 : 1
            });

            (0 == (tmp.length ? tmp[0].name: '')) && tmp.push(tmp.shift());

            return tmp;
        };
    var app = new Vue({
        el: '#index',
        data: {
            action: 'student',
            classData: [],
            teacherData:[],
        },
        methods: {
            gotoletter: function (top) {
                $('.tab-pane').animate({scrollTop: top}, 1000);
            },
            tabCheck: function (action) {
                var _this = this;
                _this.action = action;
                if (action == 'teacher') {
                    !_this.teacherData.loaded && ajax($api.schoolTeacher.list, {
                        status: 0,
                        _pagesize: 9999,
                        includeclassmaster: 1,
                        schoolid: IGrow.school.id,
                        _fields: 'id,uid,name,spell,mobile',
                        _relatedfields: 'user.avatar'
                    }, function (result) {
                        _this.teacherData.loaded = true;
                        result.data.forEach(function (item) {
                            item.avatar = item.user && item.user.avatar ? item.user.avatar : 'img/default-avatar.png';
                        });
                        _this.teacherData = spellSort(result.data || []);
                        setTimeout(function () {
                            _this.teacherData.forEach(function (item) {
                                item.top = $('#' + _this.action + item.name).offset().top - 50;
                            })
                        })
                    });
                }
            },
            init: function () {
                var _this = this;
                ajax($api.classStudent.list, {
                    schoolid: IGrow.school.id,
                    classid:utilities.routeParams.classid,
                    _pagesize: 9999,
                    includeclassmaster:1,
                    _fields:'id,uid,name,spell,mobile',
                    _relatedfields:'user.avatar'
                }, function (result) {
                    result.data.forEach(function (item) {
                        item.avatar = item.user && item.user.avatar ? item.user.avatar : 'img/default-avatar.png';
                    });
                    _this.classData = spellSort(result.data || []);
                    setTimeout(function () {
                        _this.classData.forEach(function (item) {
                            item.top = $('#' + _this.action + item.name).offset().top - 50;
                        })
                    })
                });
            }
        }
    });
    var IGrow = JSON.parse(sessionStorage.IGrow);
    app.init();
});